package cn.mygweb.admin.modules.tools.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;

import cn.mygweb.admin.modules.tools.mapper.EmailServerConfMapper;
import cn.mygweb.admin.modules.tools.mapper.EmailMapper;
import cn.mygweb.admin.modules.tools.pojo.Email;
import cn.mygweb.admin.modules.tools.pojo.EmailServerConf;
import cn.mygweb.admin.modules.tools.service.EmailService;
import cn.mygweb.common.utils.EmailUtil;
import tk.mybatis.mapper.entity.Example;

/** 
* <p>Title: EmailServiceImpl.java</p>
* <p>Description: </p>  
* @author gxb 
* @date 2019年7月13日 
* <p>Copyright: Copyright (c) 2019</p>
* <p>Company: www.mygweb.cn</p> 
*/
@Service
public class EmailServiceImpl implements EmailService{

	@Autowired
	private EmailServerConfMapper emailServerConfMapper;
	
	@Autowired
	private EmailMapper emailMapper;

	/* (non-Javadoc)
	 * <p>Title: saveEmailConf</p>
	 * <p>Description: </p>
	 * @param emailConf
	 * @see cn.mygweb.admin.modules.tools.service.EmailService#saveEmailConf(cn.mygweb.admin.modules.tools.pojo.EmailServerConf)
	 */
	@Override
	public void saveEmailConf(EmailServerConf emailServerConf) {
		emailServerConfMapper.insertSelective(emailServerConf);
	}

	/* (non-Javadoc)
	 * <p>Title: queryEmailConfList</p>
	 * <p>Description: </p>
	 * @param emailConf
	 * @return
	 * @see cn.mygweb.admin.modules.tools.service.EmailService#queryEmailConfList(cn.mygweb.admin.modules.tools.pojo.EmailServerConf)
	 */
	@Override
	public List<EmailServerConf> queryEmailConfList(EmailServerConf emailServerConf) {
		Example example = new Example(EmailServerConf.class);
		return emailServerConfMapper.selectByExample(example);
	}

	/* (non-Javadoc)
	 * <p>Title: updateEmailConf</p>
	 * <p>Description: </p>
	 * @param emailConf
	 * @see cn.mygweb.admin.modules.tools.service.EmailService#updateEmailConf(cn.mygweb.admin.modules.tools.pojo.EmailServerConf)
	 */
	@Override
	public void updateEmailConf(EmailServerConf emailServerConf) {
		emailServerConfMapper.updateByPrimaryKeySelective(emailServerConf);
	}
	
	/* (non-Javadoc)
	 * <p>Title: sendHtmlMail</p>
	 * <p>Description: </p>
	 * @param to
	 * @param subject
	 * @param content
	 * @throws Exception
	 * @see cn.mygweb.admin.modules.tools.service.EmailService#sendSimpleMail(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void sendHtmlMail(EmailServerConf emailServerConf, String to, String subject, String content) throws Exception {
		EmailUtil emailUtil = new EmailUtil(emailServerConf);
		emailUtil.sendHtmlMail(emailServerConf.getSenderName(), to, subject, content);
	}

	/* (non-Javadoc)
	 * <p>Title: sendInlineResourceMail</p>
	 * <p>Description: </p>
	 * @param senderName
	 * @param to
	 * @param subject
	 * @param content
	 * @param rscPath
	 * @param rscId
	 * @see cn.mygweb.admin.modules.tools.service.EmailService#sendInlineResourceMail(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void sendInlineResourceMail(EmailServerConf emailServerConf, String to, String subject, String content, String rscPath, String rscId) throws Exception {
		EmailUtil emailUtil = new EmailUtil(emailServerConf);
		emailUtil.sendInlineResourceMail(emailServerConf.getSenderName(), to, subject, content, rscPath, rscId);
	}

	/* (non-Javadoc)
	 * <p>Title: sendAttachmentsMail</p>
	 * <p>Description: </p>
	 * @param emailConf
	 * @param to
	 * @param subject
	 * @param content
	 * @param filePath
	 * @throws Exception
	 * @see cn.mygweb.admin.modules.tools.service.EmailService#sendAttachmentsMail(cn.mygweb.admin.modules.tools.pojo.EmailServerConf, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void sendAttachmentsMail(EmailServerConf emailServerConf, String to, String subject, String content, String filePath)
			throws Exception {
		EmailUtil emailUtil = new EmailUtil(emailServerConf);
		emailUtil.sendAttachmentsMail(emailServerConf.getSenderName(), to, subject, content, filePath);
	}

	/* (non-Javadoc)
	 * <p>Title: sendMail</p>
	 * <p>Description: </p>
	 * @param emailServerConf
	 * @param to
	 * @param subject
	 * @param content
	 * @param filePath
	 * @throws Exception
	 * @see cn.mygweb.admin.modules.tools.service.EmailService#sendMail(cn.mygweb.admin.modules.tools.pojo.EmailServerConf, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void sendMail(EmailServerConf emailServerConf, String to, String subject, String content, String filePath)
			throws Exception {
		EmailUtil emailUtil = new EmailUtil(emailServerConf);
		emailUtil.sendMail(emailServerConf.getSenderName(), to, subject, content, filePath);
	}

	/* (non-Javadoc)
	 * <p>Title: saveEmail</p>
	 * <p>Description: </p>
	 * @param email
	 * @see cn.mygweb.admin.modules.tools.service.EmailService#saveEmail(cn.mygweb.admin.modules.tools.pojo.Email)
	 */
	@Override
	public void saveEmail(Email email) {
		emailMapper.insertSelective(email);
	}

	/* (non-Javadoc)
	 * <p>Title: queryEmailListPaged</p>
	 * <p>Description: </p>
	 * @param email
	 * @param pageNum
	 * @param pageSize
	 * @return
	 * @see cn.mygweb.admin.modules.tools.service.EmailService#queryEmailListPaged(cn.mygweb.admin.modules.tools.pojo.Email, java.lang.Integer, java.lang.Integer)
	 */
	@Override
	public List<Email> queryEmailListPaged(Email email, Integer pageNum, Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		Example example = new Example(Email.class);
		example.setOrderByClause("time desc");
		return emailMapper.selectByExample(example);
	}

	/* (non-Javadoc)
	 * <p>Title: selectCount</p>
	 * <p>Description: </p>
	 * @param email
	 * @return
	 * @see cn.mygweb.admin.modules.tools.service.EmailService#selectCount(cn.mygweb.admin.modules.tools.pojo.Email)
	 */
	@Override
	public Integer selectCount(Email email) {
		Example example  = new Example(Email.class);
		return emailMapper.selectCountByExample(example  );
	}

}
